﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using DotNet.Auth.Service;
using DotNet.Auth.Utility;
using DotNet.Web.Areas.AuthWeb.Controllers;
using DotNet.Helper;
using DotNet.JCore.Entity;
using DotNet.JCore.Service;

namespace DotNet.Web.Areas.Mobile.Controllers
{
    public class M_ManualRegController : M_BaseController
    {
        private ManualRegService service = new ManualRegService();
        public ActionResult Create()
        {
            var currentUser = AuthHelper.GetSessionUser();
            if (!currentUser.IsAdmin)
            {
                ViewBag.pointId = CurrentEmployee.PointId;
            }
            MarkCreate();
            return EditCore(new ManualReg
            {
                //Id = StringHelper.Guid()
            });
        }

        public ActionResult Edit(string id)
        {
            var startDate = AuthService.Param.Get("SearchStartDate", "10");
            ViewBag.StartDate = System.DateTime.Now.AddDays(Convert.ToInt32(startDate));
            var entity = service.Get(id);
            if (entity == null) return NotFound(id);
            return EditCore(entity);
        }

        private ActionResult EditCore(ManualReg entity)
        {
            return View("Edit", entity);
        }

        private ActionResult NotFound(string id)
        {
            return NotFound("获取 人员信息 错误", $"无法找到 Id = {id} 的人员信息");
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Save(string ID,string CarNO, string PassengerNumber, string ExitNumber, string Station, string DriverName, string DriverPhone, string Line, string Corp, string PointID, string PointName, string OffenceID, string OffenceName, string CheckStatus)
        {
            ManualReg entity = CreateManualReg(ID, CarNO,  PassengerNumber,  ExitNumber,  Station,  DriverName,  DriverPhone,  Line,  Corp,  PointID,  PointName,  OffenceID,  OffenceName,  CheckStatus);
            if (entity.CheckStatus == 0)
            {
                entity.OffenceId = null;
                entity.OffenceName = null;
            }
            AuthHelper.SetJCUserInfo(entity, true);
            var result = service.Save(entity, true);
            entity.Id = "";
            entity.CarNumber = "";
            return Json(result);
        }

        private ManualReg CreateManualReg(string ID,string CarNO, string PassengerNumber, string ExitNumber, string Station, string DriverName, string DriverPhone, string Line, string Corp,string PointID, string PointName, string OffenceID, string OffenceName, string CheckStatus ="0")
        {
            ManualReg entity = new ManualReg();
            entity.Id = StringHelper.Guid();
            entity.CarNumber = CarNO;
            entity.PassengerNumber = PassengerNumber;
            entity.ExitNumber = ExitNumber;
            entity.Station = Station;
            entity.DriverName = DriverName;
            entity.DriverPhone = DriverPhone;
            entity.Line = Line;
            entity.Corp = Corp;
            entity.CheckStatus = CheckStatus==""?0: Convert.ToInt32(CheckStatus);
            entity.PointId = PointID;
            entity.PointName = PointName;
            entity.OffenceId = OffenceID;
            entity.OffenceName = OffenceName;
            entity.Note += "（移动端登记）";
            return entity;
        }
    }
}